package com.tarena.lbs.business.dao.repository.impl;



import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.api.pojo.BusinessDto;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@Slf4j
public class BusinessRepositoryImpl implements BusinessRepository {

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult<BusinessVO> selectPageList(BusinessQuery query) {
        // 1. 构建查询条件
        LambdaQueryWrapper<BusinessPO> wrapper = new LambdaQueryWrapper<>();
        // 商家名称模糊查询
        if (StringUtils.isNotBlank(query.getBusinessName())) {
            wrapper.like(BusinessPO::getBusinessName, query.getBusinessName());
        }
        // 负责人电话准确查询
        if (StringUtils.isNotBlank(query.getBusinessHeadPhone())) {
            wrapper.eq(BusinessPO::getBusinessHeadPhone, query.getBusinessHeadPhone());
        }

        // 业务状态相等查询
        if (query.getBusinessStatus() != null) {  // 如果可能为 null 才需要判空
            wrapper.eq(BusinessPO::getBusinessStatus, query.getBusinessStatus());
        }

        // 创建时间范围查询
        if (query.getStartingTime() != null && query.getEndTime() != null) {
            // 闭区间查询 [startTime, endTime]
            wrapper.between(BusinessPO::getEntryTime, query.getStartingTime(), query.getEndTime());
        } else {
            // 单边条件处理
            if (query.getStartingTime() != null) {
                wrapper.ge(BusinessPO::getEntryTime, query.getStartingTime());  // 大于等于开始时间
            }
            if (query.getEndTime() != null) {
                wrapper.le(BusinessPO::getEntryTime, query.getEndTime());   // 小于等于结束时间
            }
        }
        // 3. 执行查询
        List<BusinessPO> poPage = businessMapper.selectList(wrapper);
        log.info("查询结果：{}", poPage);
        // 4. 转换为VO
        List<BusinessVO> voList = new ArrayList<>();
        for (BusinessPO po : poPage){
            BusinessVO vo = new BusinessVO();
            BeanUtils.copyProperties(po,vo);
            voList.add(vo);
        }

        BasePageQuery simplePager = new BasePageQuery(query.getPageNo(), query.getPageSize());

        // 5. 创建分页结果
        PageResult<BusinessVO> pageResult = new PageResult<>(simplePager);
        pageResult.setObjects(voList);
        pageResult.setTotal((long)poPage.size());

        return pageResult;
    }

    @Override
    public BusinessDto getBusinesById(Integer businessId) {
        log.debug("正在查询商家信息:{}",businessId);
       BusinessPO po = businessMapper.selectById(businessId);// 查询数据库
       BusinessVO vo = new BusinessVO();
       BeanUtils.copyProperties(po,vo);
       if (vo == null) {
           return null;
       }
       BusinessDto BusinessDto = new BusinessDto();
        BeanUtils.copyProperties(vo,BusinessDto);
     return BusinessDto;
    }

    @Override
    public BusinessDto getBusinessByName(String businessName) {
        log.debug("正在查询商家信息:{}",businessName);
        BusinessPO po = businessMapper.selectByName(businessName);// 查询数据库
        if (po != null) {
            BusinessDto BusinessDto = new BusinessDto();
            BeanUtils.copyProperties(po,BusinessDto);
            return BusinessDto;
        }
        return null;
    }


    @Override
    public void save(BusinessPO businessPO) {
        log.debug("正在保存商家信息:{}",businessPO);
        businessMapper.insert(businessPO);

    }

    @Override
    public BusinessStoreVO findById(Integer businessId) {
        log.debug("正在查询商家信息:{}",businessId);
        BusinessPO po = businessMapper.selectById(businessId);
        BusinessStoreVO businessStoreVO = new BusinessStoreVO();
        BeanUtils.copyProperties(po,businessStoreVO);
        return businessStoreVO;
    }

    @Override
    public void updateStatus(BusinessParam param) {
        BusinessPO businessPO = new BusinessPO();
        BeanUtils.copyProperties(param,businessPO);
        businessMapper.updateById(businessPO);
        log.debug("更新结果:{}",businessPO);
    }

}